package com.yilin.tms.message.controller;

import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.message.Bulletin;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.message.service.IBulletinService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公告控制器
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/message/bulletin/")
public class BulletinController extends BaseController {

    @Resource
    private IBulletinService iBulletinService;

    /**
     * 获取公告记录分页
     */
    @PostMapping(value = "getBulletinPage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<Bulletin>> getBulletinPage(Bulletin query, @Explain("页码") Integer page,
                                                          @Explain("条数") Integer limit) {
        PageData<Bulletin> pageData = iBulletinService.getBulletinPage(getLoginUser(), query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 修改公告
     */
    @PostMapping(value = "updateBulletin")
    @NotNull("id")
    public ReturnData<String> updateBulletin(Bulletin bulletin) {
        Bulletin localBulletin = iBulletinService.getById(bulletin.getId());
        ParamUtil.isNull(localBulletin, "未查询到该公告");
        bulletin.setUpdateTime(new Date());
        bulletin.setUpdateUserId(getLoginUser().getId());
        iBulletinService.updateById(bulletin);
        return jsonView(true, "修改成功");
    }

    /**
     * 添加公告
     */
    @PostMapping(value = "addBulletin")
    @NotNull({"content", "link"})
    public ReturnData<String> addBulletin(Bulletin bulletin) {
        if (bulletin.getRevealed() != null && bulletin.getRevealed()) {
            judgmentBulletin(1);
        }
        bulletin.setCreateTime(new Date());
        bulletin.setAddUserId(getLoginUser().getId());
        //bulletin.setZonePlatId(getLoginUser().getOrganizeId());
        //bulletin.setZonePlatName(getLoginUser().getOrganizeName());
        iBulletinService.save(bulletin);
        return jsonView(true, "添加成功");
    }

    /**
     * 删除公告
     */
    @PostMapping(value = "deleteBulletin")
    @NotNull({"bulletinIds"})
    public ReturnData<String> deleteBulletin(String[] bulletinIds) {
        List<Bulletin> bulletinList = iBulletinService.getBulletinListByIds(ArrayUtil.initList(bulletinIds));
        iBulletinService.removeBatchByIds(bulletinList);
        return jsonView(true, "删除成功");
    }

    /**
     * 上下线公告
     */
    @PostMapping(value = "showBulletin")
    @NotNull({"bulletinIds"})
    public ReturnData<String> showBulletin(String[] bulletinIds, Boolean revealed) {
        if (revealed == null) revealed = false;
        List<Bulletin> bulletinList = iBulletinService.getBulletinListByIds(ArrayUtil.initList(bulletinIds));
        if (revealed) judgmentBulletin(bulletinList.size());
        for (Bulletin bulletin : bulletinList) {
            bulletin.setRevealed(revealed);
        }
        iBulletinService.updateBatchById(bulletinList);
        return jsonView(SUCCESS_CODE, "公告" + (revealed ? "上线" : "下线") + "成功");
    }

    /**
     * 判断上线公告数量
     */
    private void judgmentBulletin(Integer sum) {
        if (!iBulletinService.judgmentBulletin(getLoginUser(), sum)) {
            throw new RuntimeException("上线公告不能超过十个");
        }
    }

}
